import http from './http'

export interface DataSource {
  _id: string
  name: string
  type: 'prometheus' | 'grafana' | 'zabbix' | 'datadog' | 'cloudwatch' | 'custom_webhook'
  description?: string
  status: 'active' | 'inactive' | 'error' | 'testing'
  enabled: boolean
  priority: number
  connection: {
    url: string
    apiKey?: string
    username?: string
    password?: string
    timeout?: number
  }
  mapping: {
    alertNameField: string
    severityField: string
    statusField: string
    timestampField: string
    descriptionField: string
  }
  healthCheck?: {
    enabled: boolean
    interval: number
  }
  metadata: {
    createdBy: string
    lastSyncAt?: string
    lastHealthCheck?: string
    totalAlertsReceived: number
    errorCount: number
  }
  createdAt: string
  updatedAt: string
}

export interface DataSourceType {
  type: string
  name: string
  description: string
  icon: string
  supported: boolean
}

/**
 * 获取所有数据源
 */
export const getDataSources = (params?: {
  type?: string
  status?: string
  enabled?: boolean
}) => {
  return http.get<any, { success: boolean; data: DataSource[]; total: number }>('/datasources', { params })
}

/**
 * 获取单个数据源
 */
export const getDataSource = (id: string) => {
  return http.get<any, { success: boolean; data: DataSource }>(`/datasources/${id}`)
}

/**
 * 创建数据源
 */
export const createDataSource = (data: Partial<DataSource>) => {
  return http.post<any, { success: boolean; data: DataSource; message: string }>('/datasources', data)
}

/**
 * 更新数据源
 */
export const updateDataSource = (id: string, data: Partial<DataSource>) => {
  return http.put<any, { success: boolean; data: DataSource; message: string }>(`/datasources/${id}`, data)
}

/**
 * 删除数据源
 */
export const deleteDataSource = (id: string) => {
  return http.delete<any, { success: boolean; message: string }>(`/datasources/${id}`)
}

/**
 * 测试数据源连接
 */
export const testDataSource = (idOrData: string | Partial<DataSource>) => {
  const id = typeof idOrData === 'string' ? idOrData : 'new'
  const data = typeof idOrData === 'string' ? undefined : idOrData

  return http.post<any, {
    success: boolean
    message: string
    details?: {
      connectionTest: boolean
      authTest: boolean
      dataFetchTest: boolean
      latency: number
      sampleAlerts?: any[]
      errors?: string[]
    }
  }>(`/datasources/${id}/test`, data)
}

/**
 * 启用/禁用数据源
 */
export const toggleDataSource = (id: string) => {
  return http.patch<any, { success: boolean; data: DataSource; message: string }>(`/datasources/${id}/toggle`)
}

/**
 * 获取数据源统计信息
 */
export const getDataSourceStats = (id: string) => {
  return http.get<any, {
    success: boolean
    data: {
      id: string
      name: string
      type: string
      status: string
      alertsReceived: number
      lastSyncAt?: string
      averageLatency: number
      errorRate: number
      successRate: number
    }
  }>(`/datasources/${id}/stats`)
}

/**
 * 从指定数据源获取告警
 */
export const fetchAlertsFromSource = (id: string, params?: {
  startTime?: string
  endTime?: string
}) => {
  return http.get<any, { success: boolean; data: any[]; total: number }>(`/datasources/${id}/alerts`, { params })
}

/**
 * 获取支持的数据源类型
 */
export const getDataSourceTypes = () => {
  return http.get<any, { success: boolean; data: DataSourceType[] }>('/datasources/types')
}

/**
 * 同步所有数据源
 */
export const syncAllDataSources = () => {
  return http.post<any, { success: boolean; data: any[]; total: number; message: string }>('/datasources/sync')
}
